--[[
 -- *名称:GameEffectLayer
 -- *描述:全屏特效层
 -- *版权:Copyright © 2016-2018 深圳市三只小熊科技有限公司 版权所有
 -- *作者:Administrator
 -- *创建日期:2017-10-20
 -- *修改日期:2017-10-20
 ]]
local CURRENT_MODULE_NAME = ...
local GameEffectLayer = class("GameEffectLayer",PokerViewBase)


--[[
 界面处理需要保证就算是错误数据也做到不崩溃
 在获取到数据的时候进行checkData操作
]]
function GameEffectLayer:onCreate()
     self:initWithFilePath("gameeffectlayer", CURRENT_MODULE_NAME);
end


--返回键事件
function GameEffectLayer:backEvent()
 	-- GameEffectLayer.super.backEvent(self);
end
---
 -- 播放开始动画
 -- @DateTime 2017-10-20
 -- @return   [description]
 --
function GameEffectLayer:_playGameStartAnim( animData )
    local animPath = self.res_base .. "/kaiju/kaiju_effect";
    local currentHand = animData.currentHand or 1; --当前第几局
    local animNode = Util.createSpineAnimation(animPath, currentHand .. "", nil, true, animData.callback);
   	animNode:setPosition(display.center);
    self.node.root:addChild(animNode);
    SoundMgr.playSound(SoundMgr.SoundId.EFFECT_GAME_START);
    return animNode;
end
---
 -- 播放顺子动画
 -- @DateTime 2017-10-20
 -- @return   [description]
 --
function GameEffectLayer:_playShunZiAnim( animData )
    local animPath = self.res_base .. "/shunziliandui/shunzi_effect";
    local animNode = Util.createSpineAnimation(animPath, "1", nil, true, animData.callback);
    if animData.viewId then 
    	local cfg = DdzGameConfig.OutCardAnimConfig[animData.viewId];
   		animNode:setPosition(cfg.pos);
    else 
    	animNode:setPosition(display.center);
    end
    self.node.root:addChild(animNode);
    SoundMgr.playSound(SoundMgr.SoundId.EFFECT_LINE);
    return animNode;
end
---
 -- 播放连对动画
 -- @DateTime 2017-10-20
 -- @return   [description]
 --
function GameEffectLayer:_playDoubleLineAnim( animData ) 
    local animPath = self.res_base .. "/shunziliandui/shunzi_effect";
    local animNode = Util.createSpineAnimation(animPath, "2", nil, true, animData.callback);
    if animData.viewId then 
    	local cfg = DdzGameConfig.OutCardAnimConfig[animData.viewId];
   		animNode:setPosition(cfg.pos);
    else 
    	animNode:setPosition(display.center);
    end
    self.node.root:addChild(animNode);
    SoundMgr.playSound(SoundMgr.SoundId.EFFECT_LINE);
    return animNode;
end
---
 -- 播放火箭动画
 -- @DateTime 2017-10-20
 -- @return   [description]
 --
function GameEffectLayer:_playRocketAnim( animData )
    Log.d("GameEffectLayer:_playRocketAnim callback: ", animData.callback);
	SoundMgr.playSound(SoundMgr.SoundId.EFFECT_ROCKET);
    xxtimer.delay(0.5, function()
    	local animPath = self.res_base .. "/huojian/huojian_effect";
	    local animNode = Util.createSpineAnimation(animPath, "1", nil, true, animData.callback);
	    animNode:setPosition(display.center);
	    self.node.root:addChild(animNode);
	    xxtimer.delay(0.3, function()
	        EventMgr.dispatchEvent(DdzEventConfig.EVENT_SHAKE_DESKTOP);
	    end);
    	return animNode;
    end);
end
---
 -- 播放炸弹动画
 -- @DateTime 2017-10-20
 -- @return   [description]
 --
function GameEffectLayer:_playBombAnim( animData )
    local animPath = self.res_base .. "/zhadan/zhadan_effect";
    local animNode = Util.createSpineAnimation(animPath, "1", nil, true, animData.callback);
    animNode:setPosition(display.center);
    self.node.root:addChild(animNode);
    SoundMgr.playSound(SoundMgr.SoundId.EFFECT_BOMB);
    return animNode;
end
---
 -- 播放春天动画
 -- @DateTime 2017-10-20
 -- @return   [description]
 --
function GameEffectLayer:_playSpringAnim( animData )
    local animPath = self.res_base .. "/chuntian/chuntian_effect";
    local animNode = Util.createSpineAnimation(animPath, "1", nil, true, animData.callback);
    animNode:setPosition(display.center);
    self.node.root:addChild(animNode);
    SoundMgr.playSound(SoundMgr.SoundId.EFFECT_LINE);
    return animNode;
end
---
 -- 播放飞机动画
 -- @DateTime 2017-10-20
 -- @return   [description]
 --
function GameEffectLayer:_playAirPlaneAnim( animData )
	local animPath = self.res_base .. "/feiji/feiji_effect";
	local animNode = Util.createSpineAnimation(animPath, "1", nil, true, animData.callback);
    animNode:setPosition(display.center);
	self.node.root:addChild(animNode);
    SoundMgr.playSound(SoundMgr.SoundId.EFFECT_AIR_PLANE);
end
---
 -- 播放游戏结束动画
 -- @DateTime 2017-10-20
 -- @params params : { isWin: boolean, isLandlord: boolean }
 -- @return   [description]
 --
function GameEffectLayer:_playGameOverAnim( animData )
	Log.d("GameEffectLayer:_playGameOverAnim animData: ", animData);
    local animPath = "";
    local animName = animData.isLandlord and 1 or 2;
    if animData.isWin then 
        animPath = self.res_base .. "/dizhushengli/dizhushengli_effect";
    else 
        animPath = self.res_base .. "/dizhushibai/dizhushibai_effect";
    end
    local animNode = Util.createSpineAnimation(animPath, animName, nil, true, function()
    	animData.delay = animData.delay or 0;
        if animData.callback then 
        	xxtimer.delay(animData.delay, function()
        		Log.d("animPath: ", animPath);
            	animData.callback();
        	end);
        end
    end);
    animNode:setPosition(display.center);
    self.node.root:addChild(animNode);
    local soundId = animData.isWin and SoundMgr.SoundId.EFFECT_GAME_WIN or SoundMgr.SoundId.EFFECT_GAME_LOSE;
    SoundMgr.playSound(soundId);
end
---
 -- 播放游戏动画
 -- @DateTime 2017-10-23
 -- @param animType: 动画类型
 -- @param animData: 传递数据
 -- @return nil
function GameEffectLayer:playGameEffect( animType, animData )
	for k, func in pairs(self.AnimFuncMap or {}) do 
		if k == animType then 
            if type(func) == "function" then 
                func(self, animData);
            end
			break;
		end
	end
end

GameEffectLayer.AnimFuncMap = {
	[DdzGameConfig.AnimType.GAME_START]		= GameEffectLayer._playGameStartAnim;
	[DdzGameConfig.AnimType.BOMB]			= GameEffectLayer._playBombAnim;
	[DdzGameConfig.AnimType.ROCKET]			= GameEffectLayer._playRocketAnim;
	[DdzGameConfig.AnimType.SPRING]			= GameEffectLayer._playSpringAnim;
    [DdzGameConfig.AnimType.AIR_PLANE]      = GameEffectLayer._playAirPlaneAnim;
    [DdzGameConfig.AnimType.GAME_OVER]      = GameEffectLayer._playGameOverAnim;
    [DdzGameConfig.AnimType.SHUN_ZI]        = GameEffectLayer._playShunZiAnim;
    [DdzGameConfig.AnimType.DOUBLE_LINE]    = GameEffectLayer._playDoubleLineAnim;
};

return GameEffectLayer